package cn.tedu.supermarket.service.impl;

import cn.tedu.supermarket.entity.CartDetail;
import cn.tedu.supermarket.entity.CartVO;
import cn.tedu.supermarket.entity.User;
import cn.tedu.supermarket.mapper.CartMapper;
import cn.tedu.supermarket.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class ICartServiceImpl implements ICartService {

    @Autowired
    CartMapper cartMapper;
    @Override
    public Integer addCart(int id) {
        CartVO cartVO = new CartVO();
        //获取goodsId
        cartVO.setGoodsId(id);
        //获取userId
        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        System.err.println("user="+user);
        cartVO.setUserId(user.getId());
//        cartVO.setUserId(1);
        //sum = 1;
        cartVO.setNum(1);
        int sumPrice = cartMapper.selPriceById(id);
        cartVO.setSumPrice(sumPrice);
        System.err.println("cartVO="+cartVO);
        //将数据添加到数据库
        int rows = cartMapper.goodsInfoCart(cartVO);
        return rows;
    }

    @Override
    public List<CartDetail> cartDetail(int id) {
        List<CartDetail> list = cartMapper.cartDetail(id);
        return list;
    }


}
